var product_list = require('../main/product_data/product_data.js')
var app =getApp()
// pages/main/main.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hasAuth: false
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.Postid==1){
      var select = "直接1"
    }
    else
    {
      var select = "直接2"
    }
    wx.showShareMenu({
      // 要求小程序返回分享目标信息
      withShareTicket: true
    })
    var that = this
    var product_data = product_list.product_data;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log(app.globalData.userInfo)
              that.setData({
                hasAuth : true
              })
              //用户已经授权过
            }
          })
        }
      }
    })
    this.setData({
      product_data : product_data,
      select : select,
      price : 0, 
      num :0,
      swiperCurrent : 0,
      payDetail:{
        price: 0,
        num: 0,
        swiperCurrent: 0,
        product_data : {}
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '心意达达',
      path: `pages/main/main`,
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
        var shareTickets = res.shareTickets;
        // if (shareTickets.length == 0) {
        //   return false;
        // }
        // //可以获取群组信息
        // wx.getShareInfo({
        //   shareTicket: shareTickets[0],
        //   success: function (res) {
        //     console.log(res)
        //   }
        // })
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }

  },
  onSelectWay :function(){
    var that = this
    wx.showActionSheet({
      itemList: ["直接","定时","满人数"],
      success:function(res){
        var itemList = ["直接", "定时", "满人数"]
        var tapIndex = res.tapIndex
        var tapData = itemList[tapIndex]
        if(!res.cancel){
          that.setData({
            select: tapData,
            
          })
        }
      }
    })
  },
  onSelectProduct: function (e) {
    var that = this
    var currentIndex = e.detail.current
    var product_price = that.data.product_data[currentIndex].price
    that.setData({
      price: 0,
      swiperCurrent: currentIndex,
      num : 0
    })
  },
  onTapProduct: function(e){
    
    var postId = e.target.dataset.postid;
    console.log(postId)
    wx.navigateTo({
      url: "product-detail/product-detail?id=" + postId
    })
  },
  onAddProduct : function(e){
    var that = this
    that.setData({
      num: that.data.num+1,
      price: that.data.price + that.data.product_data[that.data.swiperCurrent].price
    })
  },
  onReduceProduct: function(e){
    var that = this
    var n = that.data.num
    var p = that.data.price
    if(n!=0){
      n -= 1
      p -= that.data.product_data[that.data.swiperCurrent].price
    }
    else{

    }
    that.setData({
      num: n,
      price: p
    })
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  onPay : function(){
    var that = this
    that.data.payDetail.swiperCurrent = that.data.swiperCurrent
    that.data.payDetail.price  = that.data.price
    that.data.payDetail.num  = that.data.num
    that.data.payDetail.product_data = that.data.product_data[that.data.swiperCurrent]
    var payDetail = JSON.stringify(this.data.payDetail);
    wx.navigateTo({
      url: "../paySuccess/paySuccess?payDetail=" + payDetail
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo)
    var that = this
    if (e.detail.userInfo) {
      that.setData({
        hasAuth : true
      })
      //用户按了允许授权按钮
    } else {
      //用户按了拒绝按钮
    }
  }
}
  
)